const _getZIndex = (): (() => number) => {
  let zIndex = 2000
  return () => zIndex++
}

/** 获取当前zIndex层级
 */
export const getZIndex: () => number = _getZIndex()

/** 获取不同单位下的真实px值
 * @param size 带单位数值
 * @param defaultSize 默认值 当size异常时 备用
 * @param widthOrHeight 宽高 确定%数下 选择宽高做比值
 */
export const getPropsSize = (
  size: string,
  defaultSize: number,
  widthOrHeight: 'width' | 'height' = 'width'
): number => {
  const winW = document.documentElement.clientWidth
  const winH = document.documentElement.clientHeight
  if (!Number.isNaN(Number(size))) {
    return Number(size)
  } else if (size.includes('rem')) {
    return Number(size.replace(/rem/g, ''))
  } else if (size.includes('%')) {
    const count = Number(size.replace(/%/g, ''))
    if (widthOrHeight === 'width') return (count * winW) / 100
    if (widthOrHeight === 'height') return (count * winH) / 100
  } else if (size.includes('vw')) {
    const count = Number(size.replace(/vw/g, ''))
    return (count * winW) / 100
  } else if (size.includes('vh')) {
    const count = Number(size.replace(/vh/g, ''))
    return (count * winH) / 100
  }
  return defaultSize
}

/** 时间格式化
 * @param {String} type 转时间方式  YYYY=年2019 YY=年19 MM=月 DD=天 hh:时 mm:分 ss:秒
 * @param {Date} val
 * @return {String} 格式换时间
 * */
export const dateFormat = (type: 'year' | 'month' | 'day' | string, val?: Date | string | number): string => {
  let date: Date = new Date()
  if (val) {
    date = new Date(val)
  } else {
    return ''
  }
  const YYYY = String(date.getFullYear())
  const YY = String(date.getFullYear()).substr(2)
  const m = date.getMonth() + 1
  const MM = m > 9 ? `${m}` : '0' + m
  const d = date.getDate()
  const DD = d > 9 ? `${d}` : '0' + d
  const h = date.getHours()
  const hh = h > 9 ? `${h}` : '0' + h
  const $m = date.getMinutes()
  const mm = $m > 9 ? `${$m}` : '0' + $m
  const s = date.getSeconds()
  const ss = s > 9 ? `${s}` : '0' + s
  const obj: { [key: string]: string } = {
    YYYY,
    YY,
    MM,
    DD,
    hh,
    mm,
    ss
  }
  if (type === 'year') return YYYY
  if (type === 'month') return `${YYYY}-${MM}`
  if (type === 'day') return `${YYYY}-${MM}-${DD}`
  return type.replace(/(YYYY)|(YY)|(MM)|(DD)|(hh)|(mm)|(ss)/g, (key: string) => obj[key])
}

/**
 * 哈希数据 转为 xxxxxx...xxxx
 * @param text
 * @param arr
 */
export const formatHash = (text: string, arr: number[] = [6, 4]) => {
  if (!text) return ''
  return text.replace(new RegExp('(.{' + arr[0] + '}).*(.{' + arr[1] + '})'), '$1...$2')
}

/** 保留小数点后几位
 * @param str 要保留的数据
 * @param num 保留的位数
 */
export const fixed = (str: number | string, num: number) => {
  return Number(str).toFixed(num).replace(/0*$/, '').replace(/\.$/, '')
}

/** 随机字符
 * @param num 随机字符个数
 */
export const randomWord = (num = 10): string => {
  let randomText = 'ttt-'
  const str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
  Array.from(new Array(num)).forEach((): void => {
    randomText += str[Math.floor((str.length - 1) * Math.random())]
  })
  return randomText
}

/** 复制文本
 * @param text 文本
 */
export const copyText = (text: string): void => {
  navigator?.clipboard?.writeText(text)
  const input = document.createElement('input')
  document.body.appendChild(input)
  input.value = text
  input.select()
  document.execCommand('copy', true)
  document.body.removeChild(input)
}
